import axios from "axios";
import store from '@/store'
import router from "@/router";
import { Message } from "element-ui"
//本地
// axios.defaults.baseURL="http://127.0.0.1:8888/api/private/v1/"
//线上
axios.defaults.baseURL="http://82.156.30.240:8888/api/private/v1/"
//请求前 拦截
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
config.headers.Authorization=store.getters.token
        // 发送 请求头 token
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

  // 响应拦截
// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Do something with response data
    
    let success = [200, 201, 204]

    if (response.data.meta.msg == "无效token") {
      router.push("/login")
      store.commit("user/DEL_TOKEN")
      store.commit("user/DEL_USERNAME")
  
    } else if (success.includes(response.data.meta.status)) {
      //提示成功
      // alert(response.data.meta.msg)
      Message.success(response.data.meta.msg)
    } else {
      //提示失败
      Message.error(response.data.meta.msg)
    }

    return response.data;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });

export default axios;